define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/current/houseindexshow/index' + location.search,
                    add_url: 'xiluhouse/current/houseindexshow/add',
                    edit_url: 'xiluhouse/current/houseindexshow/edit',
                    del_url: 'xiluhouse/current/houseindexshow/del',
                    multi_url: 'xiluhouse/current/houseindexshow/multi',

                    housetype_url: 'xiluhouse/house/housetype/index',
                    housetag_url:'xiluhouse/house/housetag/index',
                    renttag_url: 'xiluhouse/rent/housetag/index',
                    rentmode_url: 'xiluhouse/rent/rental_mode/index',
                    table: 'house_index_show',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='house_type_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.housetype_url).data("primaryKey", "id").data("field", "name");
                $("input[name='house_tag_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.housetag_url).data("primaryKey", "id").data("field", "name");
            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable:true},
                        {field: 'name', title: __('Name'),operate:'LIKE'},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image,operate:false},
                        {field: 'jump_type', title: __('Jump_type'), searchList: {"1":__('Jump_type 1'),"2":__('Jump_type 2'),"3":__('Jump_type 3'),"4":__('Jump_type 4'),"5":__('Jump_type 5'),"6":__('Jump_type 6'),"7":__('Jump_type 7'),"8":__('Jump_type 8')}, formatter: Table.api.formatter.normal},
                        {field: 'house_type_id', title: __('House_type_id'),formatter: function (value, row, index) {
                            if(row.house_type_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.housetype_url+ "?id=" + row.house_type_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+row.house_type_id+'】'+row.housetype.name+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        }},
                        {field: 'house_tag_id', title: __('House_tag_id'),formatter: function (value, row, index) {
                            if(row.house_tag_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.housetag_url+ "?id=" + row.house_tag_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+row.house_tag_id+'】'+row.housetag.name+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        }},
                        {field: 'rent_tag_id', title: __('Rent_tag_id'),formatter: function (value, row, index) {
                                if(row.rent_tag_id>0){
                                    var html='';
                                    var url =$.fn.bootstrapTable.defaults.extend.renttag_url+ "?id=" + row.rent_tag_id;
                                    html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+row.rent_tag_id+'】'+row.rent_tag.name+' </a>';
                                    return html;
                                }else{
                                    return '-';
                                }
                            }},
                        {field: 'rent_mode_id', title: __('Rent_mode_id'),formatter: function (value, row, index) {
                                if(row.rent_mode_id>0){
                                    var html='';
                                    var url =$.fn.bootstrapTable.defaults.extend.rentmode_url+ "?id=" + row.rent_mode_id;
                                    html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+row.rent_mode_id+'】'+row.rent_mode.name+' </a>';
                                    return html;
                                }else{
                                    return '-';
                                }
                            }},
                        //{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh'),sortable:true},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiluhouse/current/houseindexshow/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiluhouse/current/houseindexshow/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiluhouse/current/houseindexshow/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});